﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Wicresoft.Busi.BizLogic;
using Wicresoft.Busi.Entity;
using Wicresoft.Common;

public partial class Car_VehicleApplyInfo_Week2 : BaseNormalPage
{
    public string SearchDefaultText
    {
        get
        {
            return "搜索 车辆名称";
        }
    }

    public string SearchClause
    {
        get
        {
            if (ViewState["SearchClause"] == null)
                ViewState["SearchClause"] = "1=1";
            return ViewState["SearchClause"].ToString();
        }
        set
        {
            ViewState["SearchClause"] = value;
        }
    }

    protected DataTable DTData
    {
        get
        {
            if (ViewState["DTData"] == null)
                ViewState["DTData"] = new DataTable();
            return ViewState["DTData"] as DataTable;
        }
        set
        {
            ViewState["DTData"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.tbSearch.Attributes.Add("onfocus", string.Format("clearText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onblur", string.Format("insertText(this, '{0}');", SearchDefaultText));
        this.tbSearch.Attributes.Add("onkeydown", "return searchClick();");

        if (!IsPostBack)
        {
            initVehicleType();
            tbSearch.Text = SearchDefaultText;
            this.Order.OrderColumn = "ID";
            this.Order.OrderDirection = "DESC";

            SearchClause = "1=1";
            BindData();
        }

    }

    #region 初始化车辆类型下拉框
    private void initVehicleType()
    {
        DataTable dt = BLDic_Vehicle.GetVehicleType();

        this.ddlQVehicleType.DataSource = dt;
        this.ddlQVehicleType.DataTextField = Dic_Vehicle.SQLCOL_PROPERTYNAME;
        this.ddlQVehicleType.DataValueField = Dic_Vehicle.SQLCOL_PROPERTYID;
        this.ddlQVehicleType.DataBind();
        ListItem item = new ListItem("- 请选择 -", "-1");
        this.ddlQVehicleType.Items.Insert(0, item);
    }
    #endregion

    protected override void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
        base.Pager_PageChanged(sender, e);
    }

    protected override void Order_OrderChanged(object sender, EventArgs e)
    {
        BindData();
        base.Order_OrderChanged(sender, e);
    }

    protected void btnRefresh_Click(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        int recordCount = 0;
        DTData = DataAccess.Select(typeof(vwVehicles), Pager.PageIndex, Pager.PageSize, SearchClause, string.Format("{0} {1}", this.Order.OrderColumn, this.Order.OrderDirection), ref recordCount);
        Pager.RecordCount = recordCount;

        this.rptApply.DataSource = DTData;
        this.rptApply.DataBind();
    }

    protected void rptApply_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        switch (e.CommandName)
        {
            case "Delete":
                Vehicles info = new Vehicles(int.Parse(e.CommandArgument.ToString()));
                info.Load();
                info.Delete();
                WebCommon.ShowMessage(this, "车辆删除成功");
                BindData();
                break;

        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        SearchClause = "1=1";

        if (tbSearch.Text != string.Empty && tbSearch.Text != SearchDefaultText)
            SearchClause += string.Format(" AND {0} LIKE N'%{1}%'", Vehicles.SQLCOL_VEHICLENAME, tbSearch.Text.Replace("'", "''").Replace("%", "[%]"));

        BindData();

    }

    protected void btnAdvancedSearch_Click(object sender, EventArgs e)
    {
        SearchClause = "1=1";
        if (tbTitle.Text != string.Empty)
            SearchClause += string.Format(" AND {0} LIKE N'%{1}%'", Vehicles.SQLCOL_VEHICLENAME, tbTitle.Text.Replace("'", "''").Replace("%", "[%]"));
        if (this.tbNo.Text != string.Empty)
            SearchClause += string.Format(" AND {0} LIKE N'%{1}%'", Vehicles.SQLCOL_VEHICLENO, tbNo.Text.Replace("'", "''").Replace("%", "[%]"));
        if (this.ddlQVehicleType.SelectedValue != "-1")
            SearchClause += string.Format(" AND {0}=N'{1}'", Vehicles.SQLCOL_VEHICLETYPE, this.ddlQVehicleType.SelectedValue);

        BindData();
    }

}
